﻿using System;
using System.Security.Cryptography.X509Certificates;
using System.Text;

using HttpWebSocket.Interface;

using RpcClient.Interface;

using RpcHelper;
using RpcHelper.Config;
using RpcModular;

using WebSocketGateway.Interface;

namespace WebSocketGateway.Config
{
    internal class ModularConfig : IModularConfig
    {
        private IAccreditService _Accredit;

        private readonly string _ConfigKey = null;
        public ModularConfig(IApiModular modular)
        {
            IConfigCollect config = LocalConfig.Local;
            this._ConfigKey = string.Concat("gateway:", modular.ServiceName);
            this.IsAccredit = config.GetValue(this._ConfigKey + ":IsAccredit", true);
            string encoding = config.GetValue(this._ConfigKey + ":RequestEncoding", WebSocketConfig.RequestEncoding);
            if (encoding != this.RequestEncoding.BodyName)
            {
                this.RequestEncoding = Encoding.GetEncoding(encoding);
            }

            this._Accredit = RpcClient.RpcClient.Unity.Resolve<IAccreditService>();
            this.SocketConfig = new HttpWebSocket.Config.WebSocketConfig(new WebSocketEvent(modular))
            {
                IsSingle = config.GetValue(this._ConfigKey + ":IsSingle", false),
                HeartbeatTime = config.GetValue(this._ConfigKey + ":HeartbeatTime", 10),
                ServerPort = config.GetValue(this._ConfigKey + ":ServerPort", 1254),
                BufferSize = config.GetValue(this._ConfigKey + ":BufferSize", 5242880),
                TcpBacklog = config.GetValue(this._ConfigKey + ":TcpBacklog", 5000)
            };
            CertificateSet cert = config.GetValue<CertificateSet>(this._ConfigKey + ":Certificate");
            if (cert != null && !cert.FileName.IsNull())
            {
                this.SocketConfig.Certificate = new X509Certificate2(cert.FileName, cert.Pwd);
            }
            this.ApiRouteFormat = config.GetValue(this._ConfigKey + ":ApiRouteFormat", WebSocketConfig.ApiRouteFormat);
            config.RefreshEvent += this._Refresh;
        }

        private void _Refresh(IConfigCollect config, string name)
        {
            if (name == this._ConfigKey || name.StartsWith(this._ConfigKey + ":"))
            {
                this.IsAccredit = config.GetValue(this._ConfigKey + ":IsAccredit", true);
                string encoding = config.GetValue(this._ConfigKey + ":RequestEncoding", WebSocketConfig.RequestEncoding);
                if (encoding != this.RequestEncoding.BodyName)
                {
                    this.RequestEncoding = Encoding.GetEncoding(encoding);
                }
            }
        }
        public IWebSocketConfig SocketConfig
        {
            get;
        } = new HttpWebSocket.Config.WebSocketConfig();
        /// <summary>
        ///  Api 接口路径生成格式
        /// </summary>
        public string ApiRouteFormat
        {
            get;
            set;
        }
        public IResponseTemplate ResponseTemplate
        {
            get;
            set;
        } = new ResponseTemplate();

        public Encoding RequestEncoding
        {
            get;
            set;
        } = Encoding.UTF8;

        public bool IsAccredit { get; private set; }


        public IUserState GetAccredit(Guid accreditId)
        {
            return this._Accredit.GetAccredit(accreditId);
        }

        public void SetCurrentAccredit(Guid accreditId)
        {
            this._Accredit.SetCurrentAccredit(accreditId);
        }
    }
}
